﻿using System;
using System.Collections;

namespace RAP.Framework.Libary.ServerCacheManager.CacheProxy
{
  public class CacheObjectBase : ICacheEvent
  {
    public CacheObjectBase(string name)
    {
      this.Name = name;
    }

    /// <summary>
    /// 缓存被清空的事件
    /// </summary>
    public event Action<IDictionary> OnCacheCleared;

    /// <summary>
    /// 缓存被添加了的时间
    /// </summary>
    public event Action<IDictionary> OnCacheAdded;

    /// <summary>
    /// 缓存被移除了的事件
    /// </summary>
    public event Action<IDictionary> OnCacheRemoved;

    /// <summary>
    /// 异常事件
    /// </summary>
    public event Action<IDictionary> OnErrorEvent;

    /// <summary>
    /// 触发异常事件
    /// </summary>
    /// <param name="ex"></param>
    protected virtual void fireErrorEvent(object key, object value, Exception ex)
    {
      if (this.OnErrorEvent != null)
      {
        this.OnErrorEvent(new Hashtable() { { "Key", key }, { "Value", value }, { "Name", this.Name }, { "error", ex } });
      }
    }

    /// <summary>
    /// 触发异常事件
    /// </summary>
    /// <param name="ex"></param>
    protected virtual void fireErrorEvent(IDictionary exc)
    {
      if (this.OnErrorEvent != null)
      {
        this.OnErrorEvent(exc);
      }
    }

    /// <summary>
    /// 触发缓存被添加了项的事件
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    protected virtual void fireCacheAddedEvent(object key, object value)
    {
      if (this.OnCacheAdded != null)
      {
        this.OnCacheAdded(new Hashtable() { { "Key", key }, { "Value", value }, { "Name", this.Name } });
      }
    }

    /// <summary>
    /// 触发缓存被移除了项的事件
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    protected virtual void fireCacheRemovedEvent(object key, object value)
    {
      if (this.OnCacheRemoved != null)
      {
        this.OnCacheRemoved(new Hashtable() { { "Key", key }, { "Value", value }, { "Name", this.Name } });
      }
    }

    /// <summary>
    /// 触发缓存被移除了项的事件
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    protected virtual void fireCacheClearedEvent()
    {
      if (this.OnCacheCleared != null)
      {
        this.OnCacheCleared(new Hashtable() { { "Name", this.Name } });
      }
    }

    public string Name { get; set; }
  }
}
